package com.ansion.commonchat.contact.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ansion.baseui.base.TVPBaseFragment;
import com.ansion.commonchat.R;
import com.ansion.commonchat.contact.core.ContactsPresenter;
import com.ansion.log.VLog;

/**
 * tab - contact
 */
public class ContactsFragmentNew extends TVPBaseFragment<ContactsPresenter> {
    private static final String TAG = "ContactsFragmentNew";

    private boolean isVisibleToUser = false;
    private boolean firstLoadFinish = false;

    private ContactsUIProxy mUIProxy;
    private ContactsAdapterUIProxy mAdapterUIProxy;

    @Override
    protected int initContentView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        VLog.stopD("TAG_LAZY_LOAD", "%s ContactsFragmentNew initContentView", "TAG_LAZY_LOAD");
        return R.layout.fragment_main_contact_new;
    }


    @NonNull
    @Override
    public ContactsPresenter createPresenter() {
        return new ContactsPresenter();
    }

    @Override
    protected void onAddUIProxy() {
        VLog.stopD("TAG_LAZY_LOAD", "%s ContactsFragmentNew onCreate", "TAG_LAZY_LOAD");
        addUIProxy(mUIProxy = new ContactsUIProxy(this));
        addUIProxy(mAdapterUIProxy = new ContactsAdapterUIProxy(this));
        VLog.stopD("TAG_LAZY_LOAD", "%s ContactsFragmentNew onCreate 1", "TAG_LAZY_LOAD");
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        VLog.stopD("TAG_LAZY_LOAD", "%s ContactsFragmentNew onCreate 2", "TAG_LAZY_LOAD");
        lazyLoadIfNeed();
        VLog.stopD("TAG_LAZY_LOAD", "%s ContactsFragmentNew onCreate end", "TAG_LAZY_LOAD");
    }

    @Override
    public void onResumeFragment() {
        this.isVisibleToUser = true;
        lazyLoadIfNeed();
        VLog.stopD("TAG_LAZY_LOAD", "%s ContactsFragmentNew onResume", "TAG_LAZY_LOAD");
    }

    @Override
    public void onPauseFragment() {
        VLog.i("TAG_LAZY_LOAD", TAG + ",onPauseFragment,isResumed:" + isResumed());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        VLog.i("TAG_LAZY_LOAD", TAG + ",onHiddenChanged:" + hidden + ",isResumed:" + isResumed());
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        lazyLoadIfNeed();
        VLog.i("TAG_LAZY_LOAD", TAG + ",setUserVisibleHint:" + isVisibleToUser + ",isResumed:" + isResumed());

//        if(isVisibleToUser){
//            EventBus.INSTANCE.post(new Event(BConstant.App.NAVIGATION_ACTION)
//                    .putExtra(BConstant.Bundle.key_tab_position, BConstant.App.TAB_CONTACTS_POSITION)
//                    .setThreadMode(EventBusThreadMode.CURRENT));
//        }
    }


    private void lazyLoadIfNeed() {
        if (isVisibleToUser && !firstLoadFinish && presenterInitFinish()) {
            firstLoadFinish = true;

            mUIProxy.lazyLoad();
            mAdapterUIProxy.initView();
            mUIProxy.hideLoading();
            getPresenter().startQueryLocalContacts();
        }
    }

    public ContactsAdapterUIProxy getAdapterUIProxy() {
        return mAdapterUIProxy;
    }


    public ContactsUIProxy getUIProxy() {
        return mUIProxy;
    }

    public boolean isFirstLoadFinish() {
        return firstLoadFinish;
    }

    public boolean isVisibleToUser() {
        return isVisibleToUser;
    }
}
